Summary
In this notebook we will open a netCDF4 file from the Earth Surface Minteral Dust Source Investigation (EMIT) as an xarray.Dataset. We will then extract extract or clip to an area using a .geojson file (will also work with shapefile). The workflows outlined here will work with reflectance L2A or radiance L1B data.
Requirements:
emit_tutorials environment as the kernel for this notebook.../data/ folder.Learning Objectives
xarray.Dataset# Import Packages
import os
from osgeo import gdal
import xarray as xr
import rasterio as rio
import rioxarray as rxr
import hvplot.xarray
import holoviews as hv
import geopandas as gp
import sys
sys.path.append('../modules/')
from emit_tools import emit_xarray
Set the path to the downloaded EMIT data as an object. In this example we use an L2A Reflectance file.
fp = '../data/EMIT_L2A_RFL_001_20220903T163129_2224611_012.nc'
Open the file downloaded and defined as fp. To do this, we will use the emit_xarray function from the emit_tools module. This module contains a few helpful functions that can be used with EMIT data.
ds = emit_xarray(fp)
ds
<xarray.Dataset>
Dimensions: (latitude: 2009, longitude: 2353, bands: 285)
Coordinates:
* latitude (latitude) float64 -39.31 -39.31 -39.31 ... -40.39 -40.4 -40.4
* longitude (longitude) float64 -62.51 -62.51 -62.51 ... -61.24 -61.24
wavelengths (bands) float32 381.0 388.4 395.8 ... 2.486e+03 2.493e+03
fwhm (bands) float32 8.415 8.415 8.415 8.415 ... 8.806 8.807 8.809
spatial_ref int32 0
Dimensions without coordinates: bands
Data variables:
reflectance (latitude, longitude, bands) float32 nan nan nan ... nan nan
Attributes: (12/38)
ncei_template_version: NCEI_NetCDF_Swath_Template_v2.0
summary: The Earth Surface Mineral Dust Source ...
keywords: Imaging Spectroscopy, minerals, EMIT, ...
Conventions: CF-1.63
sensor: EMIT (Earth Surface Mineral Dust Sourc...
instrument: EMIT
... ...
southernmost_latitude: -40.39610428069674
spatialResolution: 0.000542232520256367
spatial_ref: GEOGCS["WGS 84",DATUM["WGS_1984",SPHER...
geotransform: [-6.25120945e+01 5.42232520e-04 -0.00...
day_night_flag: Day
title: EMIT L2A Surface Reflectance 60 m V001Using the read_file() function from geopandas, read in the .geojson file containing the polygon you wish to extract. If this cell results in an error, re-run it.
#import geopandas as gp
shape = gp.read_file('../data/isla_gaviota.geojson')
shape
| geometry | |
|---|---|
| 0 | POLYGON ((-62.14758 -39.88951, -62.16900 -39.8... |
Use the clip function from rasterio to clip the dataset to polygons from the geopandas.geodataframe. Setting all_touched to True will include pixels that intersected with the edges of the polygon.
clipped = ds.rio.clip(shape.geometry.values,shape.crs, all_touched=True)
clipped
<xarray.Dataset>
Dimensions: (latitude: 140, longitude: 163, bands: 285)
Coordinates:
* latitude (latitude) float64 -39.88 -39.88 -39.88 ... -39.95 -39.95
* longitude (longitude) float64 -62.2 -62.2 -62.2 ... -62.12 -62.12 -62.12
wavelengths (bands) float32 381.0 388.4 395.8 ... 2.486e+03 2.493e+03
fwhm (bands) float32 8.415 8.415 8.415 8.415 ... 8.806 8.807 8.809
spatial_ref int32 0
Dimensions without coordinates: bands
Data variables:
reflectance (latitude, longitude, bands) float32 nan nan nan ... nan nan
Attributes: (12/38)
ncei_template_version: NCEI_NetCDF_Swath_Template_v2.0
summary: The Earth Surface Mineral Dust Source ...
keywords: Imaging Spectroscopy, minerals, EMIT, ...
Conventions: CF-1.63
sensor: EMIT (Earth Surface Mineral Dust Sourc...
instrument: EMIT
... ...
southernmost_latitude: -40.39610428069674
spatialResolution: 0.000542232520256367
spatial_ref: GEOGCS["WGS 84",DATUM["WGS_1984",SPHER...
geotransform: [-6.25120945e+01 5.42232520e-04 -0.00...
day_night_flag: Day
title: EMIT L2A Surface Reflectance 60 m V001Select a band from the clipped dataset and plot it spatially.
clipped.isel(bands=20).hvplot.image(cmap='viridis', aspect = 'equal', frame_width=500, rasterize=True)
Now we can save the clipped xarray.Dataset as a netCDF4 output that can be reopened using the xarray.open_dataset function.
clipped.to_netcdf('../data/clipped_data.nc')
# Example for Opening
# ds = xr.open_dataset('../data/clipped_data.nc')
Email: LPDAAC@usgs.gov
Voice: +1-866-573-3222
Organization: Land Processes Distributed Active Archive Center (LP DAAC)¹
Website: https://lpdaac.usgs.gov/
Date last modified: 01-04-2023
¹Work performed under USGS contract G15PD00467 for NASA contract NNG14HH33I.